package com.uenpay.oem.ui.bankcard;

import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.oem.common.RxBusEventConstant;
import com.uenpay.oem.data.modelImpl.BankCardModel;
import com.uenpay.oem.ui.main.my.MyFragment;
import com.uenpay.service.bean.BankCardInfo;
import com.uenpay.util.net.listener.HttpOnNextListener;
import com.uenpay.util.net.listener.HttpResultListener;
import com.uenpay.util.rx.CommonEvent;
import com.uenpay.util.rx.RxBus;

import java.util.List;

/**
 * Created by DELL on 2017/6/16.
 */

public class MyBankCardPresenter implements MyBankCardContract.Present {


    private final MyBankCardContract.View mView;
    private List<BankCardInfo> data;
    private BankCardModel mModel;


    //设置默认银行卡回调
    private HttpResultListener setDefaultListen = new HttpResultListener() {
        @Override
        public void onStart() {
            super.onStart();
            mView.showLoading();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mView.dismissLoading();
            mView.showToast(e.toString());
        }

        @Override
        public void onCancel() {
            super.onCancel();
            mView.dismissLoading();
        }

        @Override
        public void onComplete() {
            super.onComplete();
            loadCards();
        }
    };

    //解绑回调
    private HttpResultListener unbindListener = new HttpResultListener() {
        @Override
        public void onStart() {
            super.onStart();
            mView.showLoading();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mView.dismissLoading();
            mView.showToast(e.toString());
        }

        @Override
        public void onCancel() {
            super.onCancel();
            mView.dismissLoading();
        }

        @Override
        public void onComplete() {
            super.onComplete();
            loadCards();
            //刷新数据
            RxBus.$().post(MyFragment.TAG,new CommonEvent(RxBusEventConstant.EVENT_BANKCARD_CHANGE));
        }
    };
    //银行卡列表回调
    private HttpOnNextListener<List<BankCardInfo>> listener = new HttpOnNextListener<List<BankCardInfo>>() {
        @Override
        public void onStart() {
            mView.showLoading();
        }

        @Override
        public void onNext(List<BankCardInfo> bankCardInfos) {
           /* BankCardInfo info = new BankCardInfo();//测试数据
            info.setBankName("招商银行");
            bankCardInfos.add(info);
            bankCardInfos.add(info);
            bankCardInfos.add(info);
            bankCardInfos.add(info);
            bankCardInfos.add(info);
            bankCardInfos.add(info);
            bankCardInfos.add(info);
            bankCardInfos.add(info);*/

            data = bankCardInfos;
            mView.setData(bankCardInfos);
            mView.completeLoad();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mView.dismissLoading();
            mView.completeLoad();
        }

        @Override
        public void onComplete() {
            mView.dismissLoading();
            mView.completeLoad();

        }

    };

    public MyBankCardPresenter(MyBankCardContract.View view) {
        this.mView = view;
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDestroy() {
        data.clear();
    }

    @Override
    public void loadCards() {
        boolean isNetworkAvailable = Utils
                .isNetworkConnected(IMApplication.getAppContext());
        if (!isNetworkAvailable) {
            mView.showToast(getString(com.uenpay.im.R.string.no_network));
            return;
        }
        mModel = new BankCardModel();
        mModel.getUserBankList(listener);
    }

    private String getString(int ResId) {
        return IMApplication.getAppContext().getString(ResId);
    }

    @Override
    public void setDefaultcard(int adapterPosition) {
        BankCardInfo bankCardInfo = data.get(adapterPosition);
        int id = bankCardInfo.getUserBankId();
        mModel.defaultUserBankDetail(id + "", setDefaultListen);
    }

    @Override
    public void unbindcard(int adapterPosition) {
        BankCardInfo bankCardInfo = data.get(adapterPosition);
        int id = bankCardInfo.getUserBankId();
        int status = 0;//默认关闭
        mModel.relieveUserBankDetail(id + "", status, unbindListener);
    }
}
